home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 8599 < prev    next >
Encoding:
Text File  |  1996-08-05  |  1.7 KB  |  58 lines

  1. Newsgroups: comp.lang.c++,comp.os.ms-windows.programmer.misc
  2. Path: netcom.com!marnold
  3. From: marnold@netcom.com (Matt Arnold)
  4. Subject: Re: Compiler Bug - can call private constructors.
  5. Message-ID: <marnoldDMyJKr.JoG@netcom.com>
  6. Organization: NETCOM On-line Communication Services (408 261-4700 guest)
  7. References: <311a149f.8353477@hector> <4fookc$m05@Milpitas01.Pop.Internex.NET>
  8. Date: Sun, 18 Feb 1996 06:05:15 GMT
  9. Sender: marnold@netcom5.netcom.com
  10.  
  11. Artyom@kansmen.com (Art Shelest) writes:
  12.  
  13. >In article <311a149f.8353477@hector>, Kenn@owl-uk.co.uk (Ken Nicolson) wrote:
  14. >>Hi everyone,
  15. >>
  16. >>Running Visual C++ v4.0, I want declare the default constructor as being
  17. >>private, to ensure that the developer initialises the class with a string,
  18. >>say, or when I am implementing a Singleton, to ensure that noone else can
  19. >>create another instance of the class. However, I find that I can return (or
  20. >>throw) an object created by the default private constructor, without any
  21. >>warning from the compiler, from anywhere outside the class!
  22. >>
  23. >>>>>> SAMPLE CODE REPRODUCING PROBLEM
  24. >>
  25. >>#include <stdio.h>
  26. >>#include <iostream.h>
  27. >>
  28. >>class FOOBAR
  29. >>{
  30. >>private:
  31. >>    FOOBAR()
  32. >>    {
  33. >>        cout << "I've called a private constructor!\n";
  34. >>    }
  35. >>};
  36. >>
  37. >>
  38. >>FOOBAR foobar()
  39. >>{
  40. >>//  FOOBAR b; SYNTAX ERROR, as expected
  41. >>    return FOOBAR( );   // This is wrong
  42. >>}
  43.  
  44. >Ah, so I'm not going crazy:
  45.  
  46. >array declaration "works" too:
  47.  
  48. >   FOOBAR ms_sucks[2]; // should not compile, but does.
  49.  
  50.  
  51. Man, this is the *latest* edition of Visual C++?  Who is MS kidding? 
  52. Perhaps you should consider a competive upgrade to Borland C++ 5.0
  53. when it comes out next month.
  54.  
  55.  
  56. Sure glad he's using a compiler that gets the *basics* right,
  57. Matt
  58.